package com.atjinggu.jinggumail.cart.controller;

import com.atjinggu.jinggumail.cart.interceptor.CartInterceptor;
import com.atjinggu.jinggumail.cart.service.CartService;
import com.atjinggu.jinggumail.cart.vo.Cart;
import com.atjinggu.jinggumail.cart.vo.CartItem;
import com.atjinggu.jinggumail.cart.vo.UserInfoTo;
import com.atjinggu.jinggumail.common.vo.MemberResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author jingming
 * @Type CartController.java
 * @Desc
 * @date 2021/3/5 14:38
 */
@Controller
public class CartController {

    @Autowired
    CartService cartService;


    @GetMapping("/OrderItemList")
    @ResponseBody
    public List<CartItem> OrderItemList(){
       List<CartItem> items =   cartService.OrderItemList();
       return items;
    }

    @GetMapping("/cart.html")
    public String cartPage( Model model) throws ExecutionException, InterruptedException {
        Cart cart =  cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") String skuId, @RequestParam("count") Integer count, RedirectAttributes ra) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,count);
        //request.setAttribute("item",item);
        ra.addAttribute("skuId",skuId);
        return "redirect:http://cart.jinggumail.com/addToCartSuccess.html";
    }


    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") String skuId, Model model){
        CartItem item =  cartService.getCartSkuInfo(skuId);
        model.addAttribute("item",item);
        return "success";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") String skuId,@RequestParam("check") Integer check) {
      cartService.checkItem(skuId,check);
       return "redirect:http://cart.jinggumail.com/cart.html";
    }

    @GetMapping("/changeItemCount")
    public String changeItemCount(@RequestParam("skuId") String skuId,@RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.jinggumail.com/cart.html";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") String skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.jinggumail.com/cart.html";
    }




}